<?php
/* SVN FILE: $Id: statistics_teams_controller.php 313 2010-07-09 12:53:39Z gregory.rome $ */
/**
 * StatisticsTeams Controller.
 *
 * cBBweb : Digital Blood Bowl league interface (http://code.google.com/p/cbbweb/)
 * Copyright 2009-2010, Grégory Romé.
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright 2009-2010, Grégory Romé.
 * @link          http://code.google.com/p/cbbweb/ cBBweb Project
 * @package       cbbweb
 * @subpackage    cbbweb.controllers
 * @version       $Revision: 313 $
 * @modifiedby    $LastChangedBy: gregory.rome $
 * @lastmodified  $Date: 2010-07-09 12:53:39 +0000 (Fri, 09 Jul 2010) $
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * StatisticsTeams Controller.
 *
 * @package       cbbweb
 * @subpackage    cbbweb.controllers
 */
class StatisticsTeamsController extends AppController {

	var $name = 'StatisticsTeams';
  var $paginate = array(
		'contain' => array(
    	'Team' => array(
    		'fields' => array('Team.ID', 'Team.strName', 'Team.iValue', 'Team.idCoach'),
    		'Race' => array('LocalizedString'),
    		'Coach'
  		)
  	),
  	'order' => array('StatisticsTeam.iWins' => 'desc')
	);

	/* ------------------------------------------------------------------- */
	/**
	 * @todo Documentation
	 */
	function index() {
		$options = array();
		if($this->championship) {
			$options['StatisticsTeam.idTeam_Listing'] = $this->current['Teams'];
		}
		$statisticsTeams = $this->paginate('StatisticsTeam', $options);
		$this->set('statisticsTeams', $statisticsTeams);
	}

	/* ------------------------------------------------------------------- */
	/** Get Best Teams.
	 * @param field
	 * @param limit
	 */
	function get_bestteams($field='td', $limit=5)
	{
		if(isset($this->passedArgs['limit'])) {
			$limit = $this->passedArgs['limit'];
		}
		if(isset($this->passedArgs['field'])) {
			$field = $this->passedArgs['field'];
		}

			switch ($field) {
			case 'td':
				$field = 'Inflicted_iTouchdowns';
				$fieldName = __('TD', true);
				$fieldTitle = __('Touchdowns', true);
				break;
			case 'std':
				$field = 'Sustained_iTouchdowns';
				$fieldName = __('sTD', true);
				$fieldTitle = __('Sustained Touchdowns', true);
				$fieldOrder = 'asc';
				break;
			case 'pa':
				$field = 'Inflicted_iPasses';
				$fieldName = __('Pa', true);
				$fieldTitle = __('Passes', true);
				break;
			case 'spa':
				$field = 'Sustained_iPasses';
				$fieldName = __('sPa', true);
				$fieldTitle = __('Sustained Passes', true);
				$fieldOrder = 'asc';
				break;
			case 'inj':
				$field = 'Inflicted_iInjuries';
				$fieldName = __('Inj', true);
				$fieldTitle = __('Injuries', true);
				break;
			case 'mr':
				$field = 'Inflicted_iMetersRunning';
				$fieldName = __('MR', true);
				$fieldTitle = __('Meters Running', true);
				break;
			case 'cas':
				$field = 'Inflicted_iCasualties';
				$fieldName = __('Cas', true);
				$fieldTitle = __('Casualties', true);
				break;
			case 'dd':
				$field = 'Inflicted_iDead';
				$fieldName = __('Dd', true);
				$fieldTitle = __('Deads', true);
				break;
			case 'mp':
				$field = 'Inflicted_iMetersPassing';
				$fieldName = __('MP', true);
				$fieldTitle = __('Meters Passing', true);
				break;
			case 'ca':
				$field = 'Inflicted_iCatches';
				$fieldName = __('Ca', true);
				$fieldTitle = __('Catches', true);
				break;
			case 'int':
				$field = 'Inflicted_iInterceptions';
				$fieldName = __('Int', true);
				$fieldTitle = __('Interceptions', true);
				break;
			case 'sinj':
				$field = 'Sustained_iInjuries';
				$fieldName = __('sInj', true);
				$fieldTitle = __('Sustained Injuries', true);
				$fieldOrder = 'asc';
				break;
			case 'scas':
				$field = 'Sustained_iCasualties';
				$fieldName = __('sCas', true);
				$fieldTitle = __('Sustained Casualties', true);
				$fieldOrder = 'asc';
				break;
			case 'sdd':
				$field = 'Sustained_iDead';
				$fieldName = __('sDd', true);
				$fieldTitle = __('Sustained Deads', true);
				$fieldOrder = 'asc';
				break;
			case 'smr':
				$field = 'Sustained_iMetersRunning';
				$fieldName = __('sMR', true);
				$fieldTitle = __('Sustained Meters Running', true);
				$fieldOrder = 'asc';
				break;
			case 'smp':
				$field = 'Sustained_iMetersPassing';
				$fieldName = __('sMP', true);
				$fieldTitle = __('Sustained Meters Passing', true);
				$fieldOrder = 'asc';
				break;
			case 'smp':
				$field = 'Sustained_iTouchdowns';
				$fieldName = __('sTD', true);
				$fieldTitle = __('Sustained Touchdowns', true);
				$fieldOrder = 'asc';
				break;
			default:
				$this->Session->setFlash(__('Wrong field', true));
				return;
		}

		if(!isset($fieldOrder)) {
			$fieldOrder = 'desc';
			$options['StatisticsTeam.'.$field.' <>'] = 0;
		}

		if ($this->championship)
		{
			$options['StatisticsTeam.idTeam_Listing'] = $this->current['Teams'];
		}

		$this->StatisticsTeam->recursive = 3;
		$statisticsTeams = $this->StatisticsTeam->find('all', array(
			'fields' => array('StatisticsTeam.'.$field, 'StatisticsTeam.idTeam_Listing'),
			'order' => array(
				'StatisticsTeam.'.$field => $fieldOrder,
				'Team.iValue' => 'desc'
			),
			'limit' => $limit,
			'contain' => array(
				'Team' => array(
					'fields' => array('Team.ID', 'Team.idRaces', 'Team.strName',
						'Team.idCoach','Team.iValue'),
					'Coach' => array('fields' => array('Coach.ID', 'Coach.strName')),
					'Race' => array(
						'LocalizedString'
					)
				)
			),
			'conditions' => $options,
		));

		if (isset ($this->params['requested'])) {
			return $statisticsTeams;
		} else {
			$this->set('statisticsTeams', $statisticsTeams);
			$this->set('field', $field);
			$this->set('fieldName', $fieldName);
			$this->set('fieldTitle', $fieldTitle);
		}
	}

	/* ------------------------------------------------------------------- */
	/** Get Worst Teams.
	 * @param field
	 * @param limit
	 */
	function get_worstteams($field='td', $limit=5)
	{
		if(isset($this->passedArgs['limit'])) {
			$limit = $this->passedArgs['limit'];
		}
		if(isset($this->passedArgs['field'])) {
			$field = $this->passedArgs['field'];
		}

			switch ($field) {
			case 'td':
				$field = 'Inflicted_iTouchdowns';
				$fieldName = __('TD', true);
				$fieldTitle = __('Touchdowns', true);
				break;
			case 'std':
				$field = 'Sustained_iTouchdowns';
				$fieldName = __('sTD', true);
				$fieldTitle = __('Sustained Touchdowns', true);
				$fieldOrder = 'desc';
				break;
			case 'pa':
				$field = 'Inflicted_iPasses';
				$fieldName = __('Pa', true);
				$fieldTitle = __('Passes', true);
				break;
			case 'spa':
				$field = 'Sustained_iPasses';
				$fieldName = __('sPa', true);
				$fieldTitle = __('Sustained Passes', true);
				$fieldOrder = 'desc';
				break;
			case 'inj':
				$field = 'Inflicted_iInjuries';
				$fieldName = __('Inj', true);
				$fieldTitle = __('Injuries', true);
				break;
			case 'mr':
				$field = 'Inflicted_iMetersRunning';
				$fieldName = __('MR', true);
				$fieldTitle = __('Meters Running', true);
				break;
			case 'cas':
				$field = 'Inflicted_iCasualties';
				$fieldName = __('Cas', true);
				$fieldTitle = __('Casualties', true);
				break;
			case 'dd':
				$field = 'Inflicted_iDead';
				$fieldName = __('Dd', true);
				$fieldTitle = __('Deads', true);
				break;
			case 'mp':
				$field = 'Inflicted_iMetersPassing';
				$fieldName = __('MP', true);
				$fieldTitle = __('Meters Passing', true);
				break;
			case 'ca':
				$field = 'Inflicted_iCatches';
				$fieldName = __('Ca', true);
				$fieldTitle = __('Catches', true);
				break;
			case 'int':
				$field = 'Inflicted_iInterceptions';
				$fieldName = __('Int', true);
				$fieldTitle = __('Interceptions', true);
				break;
			case 'sinj':
				$field = 'Sustained_iInjuries';
				$fieldName = __('sInj', true);
				$fieldTitle = __('Sustained Injuries', true);
				$fieldOrder = 'desc';
				break;
			case 'scas':
				$field = 'Sustained_iCasualties';
				$fieldName = __('sCas', true);
				$fieldTitle = __('Sustained Casualties', true);
				$fieldOrder = 'desc';
				break;
			case 'sdd':
				$field = 'Sustained_iDead';
				$fieldName = __('sDd', true);
				$fieldTitle = __('Sustained Deads', true);
				$fieldOrder = 'desc';
				break;
			case 'smr':
				$field = 'Sustained_iMetersRunning';
				$fieldName = __('sMR', true);
				$fieldTitle = __('Sustained Meters Running', true);
				$fieldOrder = 'desc';
				break;
			case 'smp':
				$field = 'Sustained_iMetersPassing';
				$fieldName = __('sMP', true);
				$fieldTitle = __('Sustained Meters Passing', true);
				$fieldOrder = 'desc';
				break;
			case 'smp':
				$field = 'Sustained_iTouchdowns';
				$fieldName = __('sTD', true);
				$fieldTitle = __('Sustained Touchdowns', true);
				$fieldOrder = 'desc';
				break;
			default:
				$this->Session->setFlash(__('Wrong field', true));
				return;
		}

		if(!isset($fieldOrder)) {
			$fieldOrder = 'asc';
		} else {
			$options['StatisticsTeam.'.$field.' <>'] = 0;
		}

		if ($this->championship)
		{
			$options['StatisticsTeam.idTeam_Listing'] = $this->current['Teams'];
		}

		$this->StatisticsTeam->recursive = 3;
		$statisticsTeams = $this->StatisticsTeam->find('all', array(
			'fields' => array('StatisticsTeam.'.$field, 'StatisticsTeam.idTeam_Listing'),
			'order' => array(
				'StatisticsTeam.'.$field => $fieldOrder,
				'Team.iValue' => 'desc'
			),
			'limit' => $limit,
			'contain' => array(
				'Team' => array(
					'fields' => array('Team.ID', 'Team.idRaces', 'Team.strName',
						'Team.idCoach','Team.iValue'),
					'Coach' => array('fields' => array('Coach.ID', 'Coach.strName')),
					'Race' => array(
						'LocalizedString'
					)
				)
			),
			'conditions' => $options,
		));

		if (isset ($this->params['requested'])) {
			return $statisticsTeams;
		} else {
			$this->set('statisticsTeams', $statisticsTeams);
			$this->set('field', $field);
			$this->set('fieldName', $fieldName);
			$this->set('fieldTitle', $fieldTitle);
		}
	}
}
?>